package arithmetic.LeetCode.Reshape_the_Matrix;

/**
 * @author zhangyanqi
 * @since 1.0 2017/10/10
 *
 * https://leetcode.com/problems/reshape-the-matrix/solution/
 */
class Solution {
    public int[][] matrixReshape(int[][] nums, int r, int c) {
        int a = nums.length;
        int b = nums[0].length;
        if((r*c) != (a*b)){
            return nums;
        }
        int[][] newMatrix = new int[r][c];
        int[] temp = new int[a * b];
        for (int i = 0; i < a; i++) {
            for (int m = 0; m < b; m++) {
                temp[(b * i) + m] = nums[i][m];
            }
        }
        int tempIndex = 0;
        for (int g = 0; g < r; g++) {
            for (int h = 0; h < c; h++) {
                newMatrix[g][h] = temp[tempIndex++];
            }
        }
        return newMatrix;
    }
}